package cn.kxx.controller;


import cn.kxx.pojo.Member;
import cn.kxx.pojo.Orders;
import cn.kxx.pojo.Product;
import cn.kxx.service.OrderService;
import cn.kxx.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: yanqi
 * @Date: 14:34
 * @Desc: 产品操作
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

//    查询所有订单
@RequestMapping("findAll")
public ModelAndView findAll(@RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "size",defaultValue = "5") Integer size){

   PageHelper.startPage(page,size);
   List<Orders> list = orderService.findAll();

   PageInfo pageInfo = new PageInfo(list);
   ModelAndView mv = new ModelAndView();
   mv.setViewName("order-list");
   mv.addObject("pageInfo",pageInfo);
   return mv;


}

//查询单个订单的详情
@RequestMapping("findById")
public ModelAndView findById(Integer id){


   Orders order = orderService.findById(id);

   ModelAndView mv = new ModelAndView();
   mv.setViewName("order-show");
   mv.addObject("order",order);
   return mv;


}

// 修改订单 start
@RequestMapping("editById")
public String editById(Integer id,HttpServletRequest request){

   HttpSession session = request.getSession();
   Orders order = orderService.findById(id);
   List<Product> list = orderService.findProductList();
   List<Member> list1 = orderService.findMemberList();
   // ModelAndView mv = new ModelAndView();
   // mv.setViewName("order-edit");
   // mv.addObject("order",order);
   // mv.addObject("productList",list);

session.setAttribute("order",order);
session.setAttribute("productList",list);
session.setAttribute("memberList",list1);
   return "order-edit";


}
   //保存修改
   @RequestMapping("edit")
   public String edit(Orders orders){


   orderService.edit(orders);


   return "redirect:/order/findAll.do";

   }
// 修改订单 end



   //新建订单 start
   @RequestMapping("add")
   public String add(HttpServletRequest request){

      HttpSession session = request.getSession();
      List<Product> productList = orderService.findProductList();
      List<Member> memberList = orderService.findMemberList();
      session.setAttribute("productList",productList);
      session.setAttribute("memberList",memberList);

      return "order-add";
   }

   @RequestMapping("save")
   public String add(Orders orders){

   orderService.save(orders);


      return "redirect:/order/findAll.do";
   }




}











